//
//  ChangeThePassword.m
//  YZZX
//
//  Created by Easaa on 15/5/4.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "ChangeThePassword.h"

@interface ChangeThePassword ()<UIScrollViewDelegate,HXHttpRequestDelegate,UITextFieldDelegate>
{
    UILabel     *OldLable;                  //旧密码
    UITextField *OldPassWord;
    
    UILabel     *NewLable;                  //新密码
    UITextField *NewPassWord;
    
    UILabel     *ConfirmLable;              //确认密码
    UITextField *ConfirmPassWord;
    
    UIScrollView        *_BgScrollView;
    
    HXHttpRequest   *_httpRequest;
    
}
@end

@implementation ChangeThePassword

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    
    // UIScrollView创建
    _BgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Dev_ScreenWidth, Dev_ScreenHeight-64)];
    _BgScrollView.contentSize =CGSizeMake(Dev_ScreenWidth, Dev_ScreenHeight);
    _BgScrollView.delegate =self;
    //_BgScrollView.bounces = NO;
    [_BgScrollView setPagingEnabled:YES];
    _BgScrollView.showsHorizontalScrollIndicator = NO;
    _BgScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_BgScrollView];

    
    OldLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, Dev_ScreenWidth-20, 30)];
    OldLable.font = [UIFont boldSystemFontOfSize:16];  //UILabel的字体大小
    OldLable.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    OldLable.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    OldLable.text =@"旧密码";
    [_BgScrollView addSubview:OldLable];
    
    OldPassWord =[[UITextField alloc]initWithFrame:CGRectMake(10, 10+30+10, Dev_ScreenWidth-20, 30)];
    OldPassWord.tag =0;
    OldPassWord.delegate =self;
    [OldPassWord.layer setMasksToBounds:YES];
    [OldPassWord.layer setCornerRadius:3]; //设置矩圆角半径
    [OldPassWord.layer setBorderWidth:1.0];   //边框宽度
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
     CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0.5 });
    [OldPassWord.layer setBorderColor:colorref];//边框颜色
    [_BgScrollView addSubview:OldPassWord];
    
    NewLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    NewLable.font = [UIFont boldSystemFontOfSize:16];  //UILabel的字体大小
    NewLable.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    NewLable.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    NewLable.text =@"新密码";
    [_BgScrollView addSubview:NewLable];
    
    NewPassWord =[[UITextField alloc]initWithFrame:CGRectMake(10, 10+30+10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    NewPassWord.tag =1;
    NewPassWord.delegate =self;
    [NewPassWord.layer setMasksToBounds:YES];
    [NewPassWord.layer setCornerRadius:3]; //设置矩圆角半径
    [NewPassWord.layer setBorderWidth:1.0];   //边框宽度
    [NewPassWord.layer setBorderColor:colorref];//边框颜色
    [_BgScrollView addSubview:NewPassWord];

    

    
    ConfirmLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10+30+10+30+10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    ConfirmLable.font = [UIFont boldSystemFontOfSize:16];  //UILabel的字体大小
    ConfirmLable.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    ConfirmLable.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    ConfirmLable.text =@"确认密码";
    [_BgScrollView addSubview:ConfirmLable];
    
    ConfirmPassWord =[[UITextField alloc]initWithFrame:CGRectMake(10, 10+30+10+30+10+30+10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    ConfirmPassWord.tag =2;
    ConfirmPassWord.delegate =self;
    [ConfirmPassWord.layer setMasksToBounds:YES];
    [ConfirmPassWord.layer setCornerRadius:3]; //设置矩圆角半径
    [ConfirmPassWord.layer setBorderWidth:1.0];   //边框宽度
    [ConfirmPassWord.layer setBorderColor:colorref];//边框颜色
    [_BgScrollView addSubview:ConfirmPassWord];

    
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomButton.tag = 2014 ;
    bottomButton.frame = CGRectMake(10, 10+30+10+30+10+30+10+30+10+30+10+30+10+30, Dev_ScreenWidth-20, 44);
    [bottomButton addTarget:self action:@selector(tabBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomButton.layer setMasksToBounds:YES];
    [bottomButton.layer setCornerRadius:2]; //设置矩圆角半径
    [bottomButton.layer setBorderWidth:0];   //边框宽度
    [bottomButton setTitle:@"确认" forState:UIControlStateNormal];
    [bottomButton setTintColor:[UIColor whiteColor]];
    bottomButton.backgroundColor =RGBCOLOR(252, 78, 125);
    
    [_BgScrollView addSubview:bottomButton];
    

}


- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    
    if (theTextField == OldPassWord)
    {
        [OldPassWord resignFirstResponder];
        
    }else if (theTextField == NewPassWord)
    {
        [NewPassWord resignFirstResponder];
        
    }else if (theTextField == ConfirmPassWord)
    {
        [ConfirmPassWord resignFirstResponder];
        
    }
    
    return YES;
}



-(void)tabBarButtonClicked
{
    
    if(OldPassWord.text.length<=0&&NewPassWord.text.length<=0&&ConfirmPassWord.text.length<=0)
    {
    
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入完整"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1024;
        [AlertView show];
        return;

    }else if (OldPassWord.text.length<=5||NewPassWord.text.length<=5||ConfirmPassWord.text.length<=5)
    {
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入不少于6位的密码"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1025;
        [AlertView show];
        return;

    }else if (![NewPassWord.text isEqualToString:ConfirmPassWord.text])
    {
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"您输入的确认密码不符。"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1026;
        [AlertView show];
        return;

 
    }
    
    
    
       //列表请求
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString], @"user_id",
                               @"1",                @"site_id",
                               OldPassWord.text,    @"old_password",
                               NewPassWord.text,    @"new_password",
                               nil];

    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_MEMBER_CHANGE_PASSWORD andTag:1023];//@"system.time"];//
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

}



#pragma mark Http request delegate
- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    //tableview列表数据
    
    if(tag == 1023)
    {
        
          NSDictionary *DicJosn = [[NSDictionary alloc]init];
          DicJosn = [JsonUtil jsonToDic:responseString];
        
        if([DicJosn[@"is_success"]boolValue])
          {
              [self.navigationController popToRootViewControllerAnimated:YES];
              
          }else
          {

              UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                                  message:DicJosn[@"message"]
                                                                 delegate:self
                                                        cancelButtonTitle:@"确定"
                                                        otherButtonTitles:nil];
              
              AlertView.tag = 1024;
              [AlertView show];
              return;
          
          }
        
    }
}

- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    
}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
